<?php
include(_DOC_ROOT.'classes/Content/Content.php');
include(_DOC_ROOT.'classes/Customer/Customer.php');
include_once(_DOC_ROOT.'lib/mail/class.phpmailer.php');
$module = ($module)?$module:'register';
if(isset($module)) {
	$pobj = new Content();
	$page = $pobj->getContent($module);
	$page['images'] = getEntityImages($module);
	$_META_TITLE = $page['meta_title'];
	$_META_KEYWORD = $page['meta_keyword'];
	$_META_DESCRIPTION = $page['meta_description'];
	
	$customer = new Customer();
	//Logout
	if($module == logout){
		if($customer->logout()) {
			header("location: "._SITE_URL.'register');
			exit;
		}
	}
	//Login
	if(isset($_POST['customer_login'])) {
		if($customer->validate($_POST['email'],$_POST['password'])) {
			header("location: "._SITE_URL);
			exit;
		}
		else
			$errmsg = 'Invalid login details.';
	}
	//Registration
	if($_POST['add_profile']) { 
			$customer_arr = array();
			if(trim($_POST['name']) != '')
				$customer_arr['name'] = $_POST['name'];
			else
				$error_arr[] = 'Please Enter a valid  name';
			if(trim($_POST['email']) != ''){
				if(!$customer->check_email($_POST['email'])){
					$email_exists = 1;
					$error_arr[] = 'Email address already exists';
				}
				$customer_arr['email'] = $_POST['email'];
			}
			else
				$error_arr[] = 'Please Enter a valid email address';
			if(strlen($_POST['password']) >= 6)
				$customer_arr['password'] = $_POST['password'];
			else
				$error_arr[] = 'Please Enter a valid password minimum 6 and maximum 30 characters.';	
			
		/*	
			if($_POST['validation_string'] != $_SESSION['vstring'])
				$error_arr[] = 'Invalid vaidation string';*/
			if(count($error_arr)) {
				if($email_exists)
					$err_str = 'Email address already exists';
				else
					$err_str = 'All Fields are required.';
			}
			else if($id = $customer->customer_registration($customer_arr)){
					
					//mail template
				try {
					$mail = new PHPMailer();
					$name = $customer_arr['name'];
					$email = $customer_arr['email'];
					$link = _SITE_URL.'register?id='.base64_encode($id);
					$password = $customer_arr['password'];
					$mail = new PHPMailer();
					$mail->From       = _EMAIL;
					$mail->FromName   = "DialShaadi";
					$mail->AddAddress("$email", "$name");
					$mail->Subject    = "DialShaadi registration successfull.";
					$body = file_get_contents('template/email/registration.tpl');
					$body = eregi_replace("[\]",'',$body);
					$find = array('#USERNAME#','#PASSWORD#','#HOST_NAME#');
					$replace = array($email,$password,$link);
					$body = str_replace($find,$replace,$body);
					$mail->MsgHTML($body);
					if(!$mail->Send()) 
					{
						//die("Mailer Error: " . $mail->ErrorInfo);
					}
				}
				catch(Exception  $e){
				}
					$err_str = 'Registration Successful!.';
					//end mail	
					//$customer->customer_login($customer_arr['email'],$customer_arr['password'])
			}
	}
	
}

	$_tplName = 'register.tpl';
	$brd_url['SignIn'] = _SITE_URL.'register';
	//print_r($brd_url);

$footer_links_tpl = 'footer_main_links.tpl';
include(_SITE_TPL_PATH_.'site_main.tpl');
?>